﻿using System;
using System.IO;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Net.FastTransfer;

namespace UtilZ.Dotnet.Ex.Exceptions
{
    /// <summary>
    /// 异常辅助类
    /// </summary>
    public static class ExceptionThrowHelper
    {
        /// <summary>
        /// 抛出指定对象释放异常
        /// </summary>
        /// <param name="obj">目标对象</param>
        /// <exception cref="ObjectDisposedException">ObjectDisposedException</exception>
        public static void ThrowObjectDisposedException(this object obj)
        {
            if (obj == null)
            {
                throw new ObjectDisposedException(null);
            }
            else
            {
                if (obj is IDisposableZ && !((IDisposableZ)obj).Disposed)
                {
                    //未释放，则不做处理
                    return;
                }

                throw new ObjectDisposedException(obj.GetType().FullName);
            }
        }

        /// <summary>
        /// 抛出指定对象释放异常
        /// </summary>
        /// <param name="obj">目标对象</param>
        /// <exception cref="ObjectDisposedException">ObjectDisposedException</exception>
        public static void ThrowObjectDisposedException(this IDisposableZ obj)
        {
            if (obj == null)
            {
                throw new ObjectDisposedException(null);
            }
            else
            {
                if (obj.Disposed)
                {
                    throw new ObjectDisposedException(obj.ToString());
                }
                else
                {
                    //未释放，则不做处理
                }
            }
        }



        /// <summary>
        /// 抛出ArgumentException异常
        /// </summary>
        /// <param name="paraName">参数名</param>
        /// <exception cref="ArgumentException">ArgumentException</exception>
        public static void ThrowArgumentException(string paraName)
        {
            if (paraName == null)
            {
                throw new ArgumentException();
            }
            else
            {
                throw new ArgumentException(paraName);
            }
        }

        /// <summary>
        /// 抛出ArgumentException异常
        /// </summary>
        /// <param name="paraName">参数名</param>
        /// <param name="message">参数信息</param>
        /// <exception cref="ArgumentException">ArgumentException</exception>
        public static void ThrowArgumentException(string paraName, string message)
        {
            throw new ArgumentException(paraName, message);
        }



        /// <summary>
        /// 抛出ArgumentNullException异常
        /// </summary>
        /// <param name="paraName">参数名</param>
        /// <exception cref="ArgumentNullException">ArgumentNullException</exception>
        public static void ThrowArgumentNullException(string paraName)
        {
            if (paraName == null)
            {
                throw new ArgumentNullException();
            }
            else
            {
                throw new ArgumentNullException(paraName);
            }
        }

        /// <summary>
        /// 抛出ArgumentNullException异常
        /// </summary>
        /// <param name="paraName">参数名</param>
        /// <param name="message">参数信息</param>
        /// <exception cref="ArgumentNullException">ArgumentNullException</exception>
        public static void ThrowArgumentNullException(string paraName, string message)
        {
            throw new ArgumentNullException(paraName, message);
        }



        /// <summary>
        /// 抛出ArgumentOutOfRangeException异常
        /// </summary>
        /// <param name="paraName">参数名</param>
        /// <exception cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</exception>
        public static void ThrowArgumentOutOfRangeException(string paraName)
        {
            if (paraName == null)
            {
                throw new ArgumentOutOfRangeException();
            }
            else
            {
                throw new ArgumentOutOfRangeException(paraName);
            }
        }

        /// <summary>
        /// 抛出ArgumentOutOfRangeException异常
        /// </summary>
        /// <param name="paraName">参数名</param>
        /// <param name="message">参数信息</param>
        /// <exception cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</exception>
        public static void ThrowArgumentOutOfRangeException(string paraName, string message)
        {
            throw new ArgumentOutOfRangeException(paraName, message);
        }




        /// <summary>
        /// 抛出ObjectNotExistsException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="ObjectNotExistsException">ObjectNotExistsException</exception>
        public static void ThrowObjectNotExistsException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new ObjectNotExistsException(message);
            }
            else
            {
                throw new ObjectNotExistsException(message, innerException);
            }
        }




        /// <summary>
        /// 抛出ConnectException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="ConnectException">ConnectException</exception>
        public static void ThrowConnectException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new ConnectException(message);
            }
            else
            {
                throw new ConnectException(message, innerException);
            }
        }




        /// <summary>
        /// 抛出DataParseException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="DataParseException">DataParseException</exception>
        public static void ThrowDataParseException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new DataParseException(message);
            }
            else
            {
                throw new DataParseException(message, innerException);
            }
        }




        /// <summary>
        /// 抛出DataTransferException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="DataTransferException">DataTransferException</exception>
        public static void ThrowDataTransferException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new DataTransferException(message);
            }
            else
            {
                throw new DataTransferException(message, innerException);
            }
        }




        /// <summary>
        /// 抛出ContainerFullException异常
        /// </summary>
        /// <exception cref="ContainerFullException">ContainerFullException</exception>
        public static void ThrowContainerFullException()
        {
            throw new ContainerFullException();
        }




        /// <summary>
        /// 抛出InitializationException异常
        /// </summary>
        /// <exception cref="InitializationException">InitializationException</exception>
        public static void ThrowInitializationException()
        {
            throw new InitializationException();
        }

        /// <summary>
        /// 抛出InitializationException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="InitializationException">InitializationException</exception>
        public static void ThrowInitializationException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new InitializationException(message);
            }
            else
            {
                throw new InitializationException(message, innerException);
            }
        }





        /// <summary>
        /// 抛出ApplicationException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="ApplicationException">ApplicationException</exception>
        public static void ThrowApplicationException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new ApplicationException(message);
            }
            else
            {
                throw new ApplicationException(message, innerException);
            }
        }

        /// <summary>
        /// 抛出Exception异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="Exception">Exception</exception>
        public static void ThrowException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new Exception(message);
            }
            else
            {
                throw new Exception(message, innerException);
            }
        }




        /// <summary>
        /// 抛出NotSupportedException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="NotSupportedException">NotSupportedException</exception>
        public static void ThrowNotSupportedException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new NotSupportedException(message);
            }
            else
            {
                throw new NotSupportedException(message, innerException);
            }
        }




        /// <summary>
        /// 抛出NotImplementedException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="NotImplementedException">NotImplementedException</exception>
        public static void ThrowNotImplementedException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new NotImplementedException(message);
            }
            else
            {
                throw new NotImplementedException(message, innerException);
            }
        }



        /// <summary>
        /// 抛出FileNotFoundException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="FileNotFoundException">FileNotFoundException</exception>
        public static void ThrowFileNotFoundException(string message, string filePath, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new FileNotFoundException(message, filePath);
            }
            else
            {
                throw new FileNotFoundException(message, filePath, innerException);
            }
        }



        /// <summary>
        /// 抛出TimeoutException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="TimeoutException">TimeoutException</exception>
        public static void ThrowTimeoutException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new TimeoutException(message);
            }
            else
            {
                throw new TimeoutException(message, innerException);
            }
        }





        /// <summary>
        /// 抛出OperationCanceledException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="OperationCanceledException">OperationCanceledException</exception>
        public static void ThrowOperationCanceledException(string message = null, Exception innerException = null)
        {
            if (message == null)
            {
                if (innerException == null)
                {
                    throw new TimeoutException();
                }
                else
                {
                    throw new TimeoutException(null, innerException);
                }
            }
            else
            {
                if (innerException == null)
                {
                    throw new TimeoutException(message);
                }
                else
                {
                    throw new TimeoutException(message, innerException);
                }
            }
        }




        /// <summary>
        /// 抛出InvalidOperationException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="InvalidOperationException">InvalidOperationException</exception>
        public static void ThrowInvalidOperationException(string message, Exception innerException = null)
        {
            if (innerException == null)
            {
                throw new InvalidOperationException(message);
            }
            else
            {
                throw new InvalidOperationException(message, innerException);
            }
        }




        /// <summary>
        /// 抛出SendDataException异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="innerException">内部异常</param>
        /// <exception cref="SendDataException">SendDataException</exception>
        public static void ThrowSendDataException(string message, Exception innerException = null)
        {
            throw new SendDataException(message, innerException);
        }


    }

}
